package com.iring.util.common;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author wanggan
 */
public class DateUtil {

    private static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private static final String START_TIME_SUB = " 00:00:00";

    private static final String END_TIME_SUB = " 23:59:59";

    public static String addStartTimeSub(String startTime) {
        return startTime + START_TIME_SUB;
    }

    public static String addEndTimeSub(String endTime) {
        return endTime + END_TIME_SUB;
    }

    public static Boolean isStartAfterEnd(String startTime, String endTime) {
        LocalDate startDate = LocalDate.parse(startTime);
        LocalDate endDate = LocalDate.parse(endTime);
        return startDate.isAfter(endDate);
    }

    public static long getTimestamp() {
        Calendar cal = Calendar.getInstance();
        return cal.getTimeInMillis();
    }

    public static Date getDate() {
        return new Date();
    }

    public static Date parseToDate(String dateStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(DATE_PATTERN));
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    public static LocalDate toLocalDate(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        return instant.atZone(zoneId).toLocalDate();
    }

}
